package cn.green.core.components.store.field;

import cn.green.core.components.field.FieldTypeCom;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.green.core.components.exceptions.FieldTypeNullException;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wang.yishu
 * @version 1.0
 * @className FieldTypeStore
 * @description 属性类型仓库
 * @date 2024/11/15 14:46
 */
public class FieldTypeStore {

    public static final Map<String, FieldTypeCom> FIELD_TYPE_MAP = new HashMap<>();
    public static final JSONArray FIELD_TYPE_OPTIONS = new JSONArray();

    public static void put( FieldTypeCom fieldTypeCom) {
        FIELD_TYPE_MAP.put(fieldTypeCom.fieldTypeCode(), fieldTypeCom);
        JSONObject option = new JSONObject();
        option.put("label", fieldTypeCom.fieldTypeName());
        option.put("value", fieldTypeCom.fieldTypeCode());
        FIELD_TYPE_OPTIONS.add(option);
    }

    public static FieldTypeCom get(String fieldTypeCode) {
        if (fieldTypeCode == null || "".equals(fieldTypeCode)) {
            return null;
        }
        if (!FIELD_TYPE_MAP.containsKey(fieldTypeCode)) {
            try {
                throw new FieldTypeNullException(fieldTypeCode);
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
        return FIELD_TYPE_MAP.get(fieldTypeCode);
    }

    public static JSONArray options() {
        return FIELD_TYPE_OPTIONS;
    }
}
